import 'package:flutter/material.dart';
import 'package:carousel_slider/carousel_slider.dart';

import 'image/cached_network_Image.dart';

//----------------滚动图广告

class ImageBannerSlider extends StatefulWidget {
  final List<String> productImages;
  final bool bIsNetworkImage;

  const ImageBannerSlider(
      {Key? key, required this.productImages, this.bIsNetworkImage = false})
      : super(key: key);

  @override
  // ignore: library_private_types_in_public_api
  ProductBannerSliderState createState() => ProductBannerSliderState();
}

class ProductBannerSliderState extends State<ImageBannerSlider> {
  late CarouselController _carouselController; // 声明 CarouselController 对象

  int _current = 0;

  @override
  void initState() {
    super.initState();
    _carouselController = CarouselController(); // 初始化 CarouselController
  }

  void setCurrentPage(int index) {
    if (index >= widget.productImages.length) {
      return;
    }

    _carouselController.animateToPage(
      index,
      duration: const Duration(milliseconds: 500), // 可选：设置动画时长
      curve: Curves.easeInOut, // 可选：设置动画曲线
    );

    if (mounted) {
      setState(() {
        _current = index;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return widget.productImages.isNotEmpty
        ? Column(
            children: [
              CarouselSlider(
                carouselController: _carouselController,
                options: CarouselOptions(
                  height: 300.0,
                  enlargeCenterPage: true,
                  autoPlay: false,
                  aspectRatio: 16 / 9,
                  initialPage: _current,
                  autoPlayCurve: Curves.fastOutSlowIn,
                  enableInfiniteScroll: true,
                  autoPlayAnimationDuration: const Duration(milliseconds: 800),
                  viewportFraction: 0.95,
                  onPageChanged: (index, reason) {
                    // setState(() {
                    //   _current = index;
                    // });
                  },
                ),
                items: widget.productImages.map((imageUrl) {
                  return Builder(
                    builder: (BuildContext context) {
                      return SizedBox(
                        width: MediaQuery.of(context).size.width,
                        child: ClipRRect(
                          borderRadius: BorderRadius.circular(10.0),
                          child: !widget.bIsNetworkImage
                              ? Image.asset(
                                  imageUrl,
                                  fit: BoxFit.contain,
                                )
                              : CachedNetworkImageLoader(
                                  imageUrl: imageUrl,
                                ),
                        ),
                      );
                    },
                  );
                }).toList(),
              ),
              const SizedBox(height: 10),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: widget.productImages.map((url) {
                  int index = widget.productImages.indexOf(url);
                  return Container(
                    width: 20.0,
                    height: 5.0,
                    margin: const EdgeInsets.symmetric(horizontal: 4.0),
                    decoration: BoxDecoration(
                      shape: BoxShape.rectangle,
                      color:
                          _current == index ? Colors.black : Colors.grey[300],
                    ),
                  );
                }).toList(),
              ),
            ],
          )
        : Container();
  }
}
